package com.zj.osm.view.map.point

import android.app.Activity
import android.content.Intent
import android.os.Bundle
import androidx.recyclerview.widget.GridLayoutManager
import com.luck.picture.lib.PictureSelector
import com.luck.picture.lib.config.PictureConfig
import com.zj.osm.R
import com.zj.osm.entity.greendao.Multimedia
import com.zj.osm.mvp.MvpActivity
import com.zj.utils.utils.utilcode.KeyboardUtils
import com.zj.utils.view.image.ImageBean
import com.zj.utils.view.image.ReleaseMsgAdapter
import kotlinx.android.synthetic.main.activity_tower.*
import kotlinx.android.synthetic.main.layout_tower_content.*
import java.util.*

open class TowerActivity : MvpActivity<TowerActivityView, TowerActivityPresenter>(), TowerActivityView, ReleaseMsgAdapter.OnDeleteItemListener{

    private var photosList: List<Multimedia> = ArrayList()
    private val imageBeanList = ArrayList<ImageBean>()
    private lateinit var adapter : ReleaseMsgAdapter
    private val beanList = ArrayList<ImageBean>()

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_tower)
        KeyboardUtils.fixAndroidBug5497(this)
        initView()
    }

    private fun initView() {
        towerTitleBar.setTitle("杆塔")
        towerTitleBar.setBackImageVisiable(true)
        towerTitleBar.setRightText("取消")
        initRecycleView()

    }

    private fun initRecycleView() {
        if (photosList.isNotEmpty()) {
            for (i in photosList.indices) {
                imageBeanList.add(ImageBean(photosList[i].name, photosList[i].path))
            }
        }
        adapter = ReleaseMsgAdapter(this,imageBeanList)
        towerRvMultimedia.adapter = adapter

        val gridLayoutManager = GridLayoutManager(this, 4)
        towerRvMultimedia.layoutManager = gridLayoutManager
        adapter.setOnDeleteItemListener(this)
        //多媒体不为空时，显示RecyclerView
        towerRvMultimedia.recycledViewPool
        towerRvMultimedia.recycledViewPool.setMaxRecycledViews(0, 10)
    }

    override fun OnDeleteItem(position: Int) {
        beanList.add(imageBeanList[position])
        imageBeanList.removeAt(position)
        //FileUtils.deleteFile(imageBeanList.get(position).getPath());
        adapter.notifyDataSetChanged()
    }

    override fun createPresenter(): TowerActivityPresenter {
        return TowerActivityPresenter()
    }

    override fun createView(): TowerActivityView {
        return this
    }


    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)
        if (resultCode == Activity.RESULT_OK) {
            if (requestCode == PictureConfig.CHOOSE_REQUEST) {
                val paths = PictureSelector.obtainMultipleResult(data)
                for (i in paths.indices) {
                    imageBeanList.add(ImageBean(paths[i].cutPath, paths[i].path))
                }
                adapter.notifyDataSetChanged()
            }
        }
    }

}
